﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using HandyControl.Controls;
using Microsoft.Win32;
using WWLauncher.Constant;
using WWLauncher.Util;
using WWLauncher.ViewModel;

namespace WWLauncher.View.Other
{
    /// <summary>
    /// TextDialog.xaml 的交互逻辑
    /// </summary>
    public partial class IconInfoUrlDialog
    {
        IconInfo Source { get; set; }
        private bool newIconInfo { get; set; }
        private readonly MainViewModel mainViewModel = MainViewModel.Instance();
        public IconInfoUrlDialog()
        {
            newIconInfo = true;
            Source = new IconInfo();
            var info = new IconViewModel
            {
                BitmapImage = ImageUtil.GetBitmapIconByUri(Constants.DefaultNetwork),
            };
            info.DefaultImageArr = info.ImageByteArr;
            this.DataContext = info;
            InitializeComponent();
        }

        public IconInfoUrlDialog(IconInfo sourceinfo)
        {
            Source = sourceinfo;
            var info = sourceinfo.To();
            this.DataContext = info;
            info.DefaultImageArr = info.ImageByteArr;
            newIconInfo = false;
            InitializeComponent();
        }
        private void IconInfoUrlDialog_OnLoaded(object sender, RoutedEventArgs e)
        {

            RunTimeStatus.WpfWindowClick = true;
            RunTimeStatus.WpfWindowOpening = true;
            BGSettingUtil.BGSetting(BGBorder);
        }

        /// <summary>
        /// 保存修改属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveProperty_Click(object sender, RoutedEventArgs e)
        {
            Source.BitmapImage = IconImg.Source as BitmapImage;
            Source.Name = IconName.Text;
            Source.TargetPath = IconUrl.Text;
            Source.StartType = IconStartType.OpenWithBrowser;
            if (newIconInfo)
            {
                mainViewModel.MenuList[mainViewModel.SelectedMenuIndex].IconList.Add(Source);
            }
            ConfigUtil.SaveAppData();
          
            this.Close();
        }

        /// <summary>
        /// 修改图标为默认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReStoreImage_Click(object sender, RoutedEventArgs e)
        {
            var info = this.DataContext as IconViewModel;
            info.BitmapImage = ImageUtil.ByteArrToImage(info.DefaultImageArr);
        }
        /// <summary>
        /// 程序窗体拖拽
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DragMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }
        /// <summary>
        /// 修改图标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditImage_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog
                {
                    Multiselect = false,
                    Filter = "所有文件(*.*)|*.*"
                };
                if (ofd.ShowDialog().Value)
                {
                    var info = this.DataContext as IconViewModel;
                    info.BitmapImage = ImageUtil.GetBitmapIconByPath(ofd.FileName);
                }
            }
            catch (Exception ex)
            {
                Growl.WarningGlobal("修改图标失败!");
                LogUtil.WriteErrorLog(ex, "修改图标失败!");
            }

        }

        private void CloseBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

     
        private void Window_Closing(object sender, CancelEventArgs e)
        {
            RunTimeStatus.WpfWindowClick = false;
            RunTimeStatus.WpfWindowOpening = false;
        }
    }
}
